package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeDao goodsTypeDao;


    //查询商品所有分类
    @Override
    public List loadGoodsType() {
        Integer pId=-1;
        List<GoodsType> goodsTypeList = goodsTypeDao.loadGoodsType(pId);

        ArrayList<Object> list = new ArrayList<>();

        goodsTypeList.stream().forEach(goodsType -> {
                Map hashMap =new HashMap<>();
            hashMap.put("id",goodsType.getGoodsTypeId());
            hashMap.put("text", goodsType.getGoodsTypeName());
            hashMap.put("state", "closed");
            hashMap.put("iconCls", "goods-type");
            hashMap.put("attributes", new HashMap<>().put("state",goodsType.getGoodsTypeState()));

            //添加二级数据
            List<GoodsType> ChildrenList = goodsTypeDao.loadGoodsType(goodsType.getGoodsTypeId());
            ChildrenList.stream().forEach(goodsType1 -> {
                HashMap<String, Object> ChildrenMap = new HashMap<>();
                ChildrenMap.put("id",goodsType1.getGoodsTypeId());
                ChildrenMap.put("text", goodsType1.getGoodsTypeName());
                ChildrenMap.put("state", "closed");
                ChildrenMap.put("iconCls", "goods-type");
                ChildrenMap.put("attributes", new HashMap<>().put("state",goodsType1.getGoodsTypeState()));

                //添加三级缓存
                List<GoodsType> childrenList = goodsTypeDao.loadGoodsType(goodsType1.getGoodsTypeId());
                childrenList.stream().forEach(goodsType2 -> {
                    HashMap<String, Object> childrenMap = new HashMap<>();
                    childrenMap.put("id",goodsType2.getGoodsTypeId());
                    childrenMap.put("text", goodsType2.getGoodsTypeName());
                    childrenMap.put("state", "closed");
                    childrenMap.put("iconCls", "goods-type");
                    childrenMap.put("attributes", new HashMap<>().put("state",goodsType2.getGoodsTypeState()));
                });
                ChildrenMap.put("children", childrenList);
            });
            hashMap.put("children", ChildrenList);
        });
        return goodsTypeList;
    }
}
